package com.tubang.logistics.controller;


import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tubang.common.constant.Constants;
import com.tubang.common.util.BeanMapper;
import com.tubang.common.util.GsonQuick;
import com.tubang.common.util.ResultData;
import com.tubang.component.service.IRedisService;
import com.tubang.component.util.RequestUtil;
import com.tubang.logistics.entity.MessageEntity;
import com.tubang.logistics.service.IMessageService;
import com.tubang.logistics.vo.MessageVo;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wsl
 * @since 2017-11-21
 */
@RestController
@RequestMapping("/message")
public class MessageController {
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Resource
	private IRedisService redisService;
	@Resource
	private IMessageService messageService;
	
	/**
	 * 
	 * @Title selectMessageList 
	 * @Description 根据收消息用户id查询消息列表
	 * @param @return 设定文件 
	 * @return ResultData 返回类型 
	 * @throws
	 */
	@GetMapping("selectMessageList")
	public ResultData selectMessageList(MessageVo messageVo) {
		try {
			logger.info("[message]==> method[selectMessageList],请求数据为：[{}]", GsonQuick.getGson().toJson(messageVo));
			MessageEntity messageEntity = BeanMapper.map(messageVo, MessageEntity.class);
			EntityWrapper<MessageEntity> ew = new EntityWrapper<>(messageEntity);
			Page<MessageEntity> page = new Page<>(messageVo.getPage(),messageVo.getRows());
			
			Page<MessageEntity> selectPage = messageService.selectPage(page,ew);
			int totalCount = messageService.selectCount(ew);
			
			logger.info("[message]==> method[selectMessageList],应答：[{}]", GsonQuick.getGson().toJson(selectPage));
			return ResultData.result(true).setTotalCount(totalCount).setData(selectPage.getRecords());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("[message]==> method[selectMessageList],应答：[{}]", e.getMessage());
			return ResultData.result(false).setData(e);
		}
	}
	
	/**
	 * 
	 * @Title updateMessageStatus 
	 * @Description 点击查看后修改消息状态为已读
	 * @param  id
	 * @param  设定文件 
	 * @return ResultData 返回类型 
	 * @throws
	 */
	@PutMapping("updateMessageStatus")
	public ResultData updateMessageStatus(@RequestParam(required = true) String id) {
		try {
			MessageEntity messageEntity = new MessageEntity();
			messageEntity.setStatusFlag(Constants.DEFAULT_VAULE_ONE);
			messageEntity.setId(id);
			messageEntity.setUpdateTime(new Date());
			messageService.updateById(messageEntity);
			return ResultData.result(true);
		} catch (Exception e) {
			logger.error("[message]==> method[selectMessageList],应答：[{}]", e.getMessage());
			e.printStackTrace();
			return ResultData.result(false).setData(e);
		}
	}
	
	
	/**
	 * 
	 * @Title insertOrUpdate 
	 * @Description 发布或者修改消息
	 * @return ResultData 返回类型 
	 * @throws
	 */
	@PostMapping("insertOrUpdate")
	public ResultData insertOrUpdate(HttpServletRequest request, HttpServletResponse response) {
		String receivePostJson = RequestUtil.receivePostJson(request);
		MessageVo messageVo = GsonQuick.toObject(receivePostJson, MessageVo.class);
		try {
			logger.info("[message]==> method[insertOrUpdate],请求参数为：[{}]", GsonQuick.getGson().toJson(messageVo));
			MessageEntity messageEntity = BeanMapper.map(messageVo, MessageEntity.class);
			messageEntity.setUpdateTime(new Date());
			messageService.insertOrUpdate(messageEntity);
			logger.info("[message]==> method[insertOrUpdate],新增或者更新的消息数据为：[{}]", GsonQuick.getGson().toJson(messageVo));
			return ResultData.result(true);
		} catch (Exception e) {
			logger.error("[message]==> method[insertOrUpdate],应答：[{}]", e);
			e.printStackTrace();
			return ResultData.result(false).setData(e);
		}
	}
	
	
	
	
	
	
//	/**
//	 * 
//	 * @Title selectMessageByCondition 
//	 * @Description 根据条件查询消息列表
//	 * @param  messageVo
//	 * @param  设定文件 
//	 * @return ResultData 返回类型 
//	 * @throws
//	 */
//	@GetMapping("selectMessageByCondition")
//	public ResultData selectMessageByCondition(MessageVo messageVo, @RequestParam(required = true) String userId) {
//		try {
//			messageVo.setToUserId(userId);
//			logger.info("[message]==> method[selectMessageByCondition],请求数据为：[{}]", GsonQuick.getGson().toJson(messageVo));
//			Page<MessageEntity> page = new Page<>(messageVo.getPage(),messageVo.getRows());
//			MessageEntity entity = BeanMapper.map(messageVo, MessageEntity.class);
//			EntityWrapper<MessageEntity> wrapper = new EntityWrapper<>(entity);
//			Page<MessageEntity> selectPage = messageService.selectPage(page,wrapper);
//			logger.info("[message]<== method[selectMessageByCondition],响应数据为：[{}]", GsonQuick.getGson().toJson(selectPage));
//			return ResultData.result(true).setCode(MsgCode.SUCCESS).setMsg(MsgCode.DEFAULT_SUCCESS_MSG).setData(selectPage);
//		} catch (Exception e) {
//			e.printStackTrace();
//			logger.error("[message]<== method[selectMessageByCondition],错误原因为：[{}]", e.getMessage());
//			return ResultData.result(true).setCode(MsgCode.ERROR).setMsg(MsgCode.DEFAULT_ERROR_MSG).setData(e);
//		}
//	}
}
